# -*- coding: utf-8 -*-
# @Time    : 2024/1/11 20:39
# @Author  : micah
# @File    : 14.使用选择器完成数据提取.py
# @Software: PyCharm

import re
from bs4 import BeautifulSoup

html_str = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

soup = BeautifulSoup(html_str, 'lxml')


"""标签选择器: 在select方法中直接传递标签名查询"""
print(soup.select('title'))
print(soup.select('a'))
print(soup.select('b'))

print('-' * 30)

"""类选择器: 根据标签中的class属性值提取数据"""
# class对应的值在进行传递的时候值的前面必须加上 .
print(soup.select('.sister'))

print('-' * 30)

"""id选择器: 如果标签中存在id属性则可以根据id进行数据提取"""
# id对应的值在进行传递的时候值的前面必须加上 #
print(soup.select('#link1'))

print('-' * 30)

"""层级选择器: 可以根据标签与属性的包含关系提取数据"""
print(soup.select('p #link2'))

print('-' * 30)

"""属性选择器: 根据属性提取对应的标签"""
print(soup.select('p[class="title"]'))

